<?php
/**
 * 医帮网后台控制器基类
 * @category Tech
 * @package  Action
 * @author   tongjh
 * @version  V1.0
 */
class CommonAction extends Action {
	
    /**
     * 初始化方法检查用户登陆
     * @access public
     * @return void
     */
    public function _initialize(){
    	if(!isset($_SESSION['userArr']['name'])){
            $this->error("请登陆后访问",U('Admin/Login/index'));
        }
            //$this->_leve();
    }
	
    /**
     * 用户权限方法
     * @access public
     * @return void
     */
    public function _leve(){
        //读出该用户的管理节点
        $groupid = $_SESSION['userArr']['groupid'];
        $form1 = M('sysgroup');
        $list1 = $form1->where(array('id'=>$groupid))->find();
        $sys = explode(',', $list1['sys']);
    	if(!$list1){$this->error("没有访问权限",'/'.GROUP_NAME.'/Login/index');}
        //IP限制
        $ip = get_client_ip();
        if($list1['lockip'] && ($list1['lockip'] != $ip)){$this->error('你所在的IP不正确');}
        //得出需要访问权限的节点
        if(!S('sysjiedian')){
            $Form = M("sysjiedian");
            $list = $Form->field('id,action')->select();
            S('sysjiedian',$list);
        }
        $list = S('sysjiedian');               
        foreach ($list as $key => $value) {
            $arr[] = strtolower($value['action']);
            if(in_array($value['id'],$sys)){
                //记录当前用户可以访问的路径
                $yousys[] = strtolower($value['action']);
            }
        }
        //当前的action
        $action =  strtolower(GROUP_NAME.'/'.MODULE_NAME.'/'.ACTION_NAME);
        //添加豁免权,在项目配置文件admins一项中添加
        $user = $_SESSION['userArr']['name'];
        if(in_array($user, C('ADMINS'))){return;}
        //如果当前访问的链接出现在节点中的话，就检查
        if(in_array($action,$arr)){
            //如果当前访问的链接没有出现在本人可以访问的数组中就没有权限
            $yousys = array_merge($yousys,array('admin/index/index'));
            if(!in_array($action, $yousys)){
                //ajax方式访问后台
                if($this->isAjax()){
                    echo json_encode(array('msg'=>'没有访问权限'));exit;
                }else{
                    exit("你没有权限访问");
                }
            }            
        }                
    }
	
	
	/**
     * 记录日志
     * @access public
     * @return void
     */
    public function log_txt($state,$msg){
        $Form2 = M('Vuser');
        $user = $Form2->getByAccount($_SESSION['account']);
        $Form = M('Log_act');
        $map['userid'] = $user['id'];
        $map['leve'] = $user['leve'];
        $map['name'] = $user['name'];
        $map['state'] = $state;
        $map['act'] = $msg;
        $user = $Form->data($map)->add();	  	  
    }
    
    /*
     * 缓存更新
     */
    public function cache_del(){
        deldir(RUNTIME_PATH);
        $this->success('删除缓存成功',U('Admin/Index/right'));
    }


	// 文件上传
        protected function _upload() {
                import("@.ORG.UploadFile");
                //导入上传类
                $upload = new UploadFile();
                //设置上传文件大小
                $upload->maxSize = 3292200;
                //设置上传文件类型
                $upload->allowExts = explode(',', 'jpg,gif,png,jpeg');
                //设置附件上传目录
                $upload->savePath = '.'.C('UPLOADS');
                //设置文件上传名(按照时间)  
                $upload->saveRule = "time";  

                //设置需要生成缩略图，仅对图像文件有效
                //$upload->thumb = true;
                // 设置引用图片类库包路径
                //$upload->imageClassPath = 'ORG.Image';
                //设置需要生成缩略图的文件后缀
                //$upload->thumbPrefix = 'm_,s_';  //生产2张缩略图
                //设置缩略图最大宽度
                //$upload->thumbMaxWidth = '400,100';
                //设置缩略图最大高度
                //$upload->thumbMaxHeight = '400,100';
                //设置上传文件规则
                $upload->saveRule = uniqid;
                //删除原图
                // $upload->thumbRemoveOrigin = true;
                if (!$upload->upload()) {
                //捕获上传异常
                        $this->error($upload->getErrorMsg());
                } else {
                //取得成功上传的文件信息
                        $info = $upload->getUploadFileInfo();
                /*            import("@.ORG.Image");
                //给m_缩略图添加水印, Image::water('原文件名','水印图片地址')
                Image::water($uploadList[0]['savepath'] . 'm_' . $uploadList[0]['savename'], '../Public/Images/logo2.png');
                */            //$_POST['image'] = $uploadList[0]['savename'];

                }
                return $info;
        }
	


}
?>